package com.dnjn.happiness.survey.controller.common;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.survey.domain.CheckQuestion;
import com.dnjn.happiness.survey.domain.vo.CheckQuestionVO;
import com.dnjn.happiness.survey.service.ICheckQuestionService;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;

/**
 * 考核题目Controller
 *
 * @author happiness
 * @date 2024-04-02
 */

@Slf4j
public class CommonCheckQuestionController extends BaseController {
    @Autowired
    protected ICheckQuestionService checkQuestionService;

    /**
     * 查询考核题目列表
     */
    @Log(title = "考核题目", businessType = BusinessType.LIST)
//    @RequiresPermissions("@ss.hasAnyPermi('check:question:list,ignore:question:list')")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody CheckQuestionVO vo, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        CheckQuestion checkQuestion=new CheckQuestion();
        BeanUtils.copyPropsIgnoreNull(vo,checkQuestion);
        Page page = Page.of(current, size);
        Page<CheckQuestion> list = checkQuestionService.page(page, Wrappers.query(checkQuestion));
        return success(list);
    }

    /**
     * 获取考核题目详细信息
     */
    @Log(title = "考核题目", businessType = BusinessType.SELECT)
//    @RequiresPermissions("@ss.hasAnyPermi('check:question:query,ignore:question:query')")
    @GetMapping(value = "/{questionId}")

    public AjaxResult get(@PathVariable("questionId") Long questionId) {
        return success(checkQuestionService.getById(questionId));
    }

    /**
     * 新增考核题目
     */
    @Log(title = "考核题目", businessType = BusinessType.INSERT)
//    @RequiresPermissions("@ss.hasAnyPermi('check:question:add,ignore:question:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CheckQuestionVO vo) {
        CheckQuestion checkQuestion=new CheckQuestion();
        BeanUtils.copyPropsIgnoreNull(vo,checkQuestion);

        checkQuestion.setCreateBy(SecurityUtils.getUsername());
        return toAjax(checkQuestionService.save(checkQuestion));
    }

    /**
     * 修改考核题目
     */
    @Log(title = "考核题目", businessType = BusinessType.UPDATE)
//    @RequiresPermissions("@ss.hasAnyPermi('check:question:edit,ignore:question:edit')")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody CheckQuestionVO vo) {
        CheckQuestion checkQuestion=new CheckQuestion();
        BeanUtils.copyPropsIgnoreNull(vo,checkQuestion);
        return toAjax(checkQuestionService.updateById(checkQuestion));
    }

    /**
     * 删除考核题目
     */
    @Log(title = "考核题目", businessType = BusinessType.DELETE)
//    @RequiresPermissions("@ss.hasAnyPermi('check:question:remove,ignore:question:remove')")
    @DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable Long[] questionIds) {
        return toAjax(checkQuestionService.removeBatchByIds(Arrays.asList(questionIds)));
    }


    /**
     * 获取考核题目详细信息
     */
    @Log(title = "考核题目", businessType = BusinessType.SELECT)
//    @RequiresPermissions("@ss.hasAnyPermi('check:question:query,ignore:question:query')")
    @GetMapping(value = "/getQuestionList")

    public AjaxResult getQuestionList( Long checkId) {
        List<CheckQuestion> questions=checkQuestionService.getQuestionList(checkId);
        return success(questions);
    }
}
